package com.tianbing.adlottery.controller.hordeadmin;

import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.util.FileCopyUtils;
import org.springframework.validation.BindException;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.fengniu.ejb.business.IUserBusiness;
import com.fengniu.ejb.dao.ISiteDao;
import com.fengniu.ejb.entity.Site;
import com.fengniu.ejb.entity.User2Site;
import com.tianbing.adlottery.helper.SkinUtils;
import com.tianbing.adlottery.util.ControllerFormBean;
import com.tianbing.adlottery.util.UserSession;

public class UploadMyServedStoreImagesController extends SimpleFormController {
	static Logger logger = Logger.getLogger(UploadMyServedStoreImagesController.class.getName());

	private SkinUtils skinUtils;

	private String fileUploadDir = "";

	private Integer logoImageMaxSize = 10240;// 10k

	private Integer smallImageMaxSize = 102400;// 100k

	private Integer mediumImageMaxSize = 512000;// 500k

	private Integer largeImageMaxSize = 1024000;// 1M

	private String logoImageNameSuffix = "LOGO.JPG";

	private String smallImageNameSuffix = "SMALL.JPG";

	private String mediumImageNameSuffix = "MEDIUM.JPG";

	private String largeImageNameSuffix = "LARGE.JPG";

	private ISiteDao siteDao;

	private IUserBusiness userBusiness;

	/*
	 * upload served store's images
	 * 
	 * @see org.springframework.web.servlet.mvc.SimpleFormController#onSubmit(javax.servlet.http.HttpServletRequest,
	 *      javax.servlet.http.HttpServletResponse, java.lang.Object,
	 *      org.springframework.validation.BindException)
	 */
	@SuppressWarnings("unchecked")
	protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object cmd,
			BindException ex) throws Exception {

		ModelAndView mv = new ModelAndView(this.getSuccessView());
		ControllerFormBean form = (ControllerFormBean) cmd;
		UserSession auth = (UserSession) skinUtils.getUserAuthorization(request, response, true);
		Long storeSysId = ServletRequestUtils.getLongParameter(request, "ssid", -1);

		if (storeSysId > -1) {

			Map u2sMap = userBusiness.getUserServedStores(auth.getUserSysId(), storeSysId, 0, 1);
			List<User2Site> myServedStoreList = (List) u2sMap.get("user2SiteList");
			if (myServedStoreList != null && myServedStoreList.size() > 0) {
				if (form.getLogoImage() != null) {
					MultipartFile f = form.getLogoImage();
					if (0 < f.getSize() && f.getSize() <= logoImageMaxSize) {
						String logoImageFileName = fileUploadDir + storeSysId + logoImageNameSuffix;
						FileOutputStream fos = new FileOutputStream(logoImageFileName);
						FileCopyUtils.copy(f.getInputStream(), fos);
						fos.close();
					} else if (f.getSize() > logoImageMaxSize) {
						mv.getModel().put("tips", "Logo file size exceed " + logoImageMaxSize / 1024 + "K");
					}
				}
				if (form.getSmallImage() != null) {
					MultipartFile f = form.getSmallImage();
					if (0 < f.getSize() && f.getSize() <= smallImageMaxSize) {
						String smallImageFileName = fileUploadDir + storeSysId + smallImageNameSuffix;
						FileOutputStream fos = new FileOutputStream(smallImageFileName);
						FileCopyUtils.copy(f.getInputStream(), fos);
						fos.close();
					} else if (f.getSize() > smallImageMaxSize) {
						mv.getModel().put("tips", "SMALL file size exceed " + smallImageMaxSize / 1024 + "K");
					}
				}
				if (form.getMediumImage() != null) {
					MultipartFile f = form.getMediumImage();
					if (0 < f.getSize() && f.getSize() <= mediumImageMaxSize) {
						String mediumImageFileName = fileUploadDir + storeSysId + mediumImageNameSuffix;
						FileOutputStream fos = new FileOutputStream(mediumImageFileName);
						FileCopyUtils.copy(f.getInputStream(), fos);
						fos.close();
					} else if (f.getSize() > mediumImageMaxSize) {
						mv.getModel().put("tips", "MEDIUM file size exceed " + mediumImageMaxSize / 1024 + "K");
					}
				}
				if (form.getLargeImage() != null) {
					MultipartFile f = form.getLargeImage();
					if (0 < f.getSize() && f.getSize() <= largeImageMaxSize) {
						String largeImageFileName = fileUploadDir + storeSysId + largeImageNameSuffix;
						FileOutputStream fos = new FileOutputStream(largeImageFileName);
						FileCopyUtils.copy(f.getInputStream(), fos);
						fos.close();
					} else if (f.getSize() > largeImageMaxSize) {
						mv.getModel().put("tips", "LARGE file size exceed " + largeImageMaxSize / 1024 + "K");
					}
				}
			}
		}

		return mv;
	}

	@SuppressWarnings("unchecked")
	protected Map referenceData(HttpServletRequest request) throws Exception {
		Map model = new HashMap();
		Long ssid = ServletRequestUtils.getLongParameter(request, "ssid", -1);

		Site store = siteDao.findSiteBySysId(ssid);
		if (store != null) {
			model.put("storeDetail", store);
			model.put("largeImageName", store.getSysId() + largeImageNameSuffix);
			model.put("mediumImageName", store.getSysId() + mediumImageNameSuffix);
			model.put("smallImageName", store.getSysId() + smallImageNameSuffix);
			model.put("logoImageName", store.getSysId() + logoImageNameSuffix);
		}
		return model;
	}

	public static void setLogger(Logger logger) {
		UploadMyServedStoreImagesController.logger = logger;
	}

	public void setSkinUtils(SkinUtils skinUtils) {
		this.skinUtils = skinUtils;
	}

	public void setFileUploadDir(String fileUploadDir) {
		this.fileUploadDir = fileUploadDir;
	}

	public void setLogoImageMaxSize(Integer logoImageMaxSize) {
		this.logoImageMaxSize = logoImageMaxSize;
	}

	public void setSmallImageMaxSize(Integer smallImageMaxSize) {
		this.smallImageMaxSize = smallImageMaxSize;
	}

	public void setMediumImageMaxSize(Integer mediumImageMaxSize) {
		this.mediumImageMaxSize = mediumImageMaxSize;
	}

	public void setLargeImageMaxSize(Integer largeImageMaxSize) {
		this.largeImageMaxSize = largeImageMaxSize;
	}

	public void setLogoImageNameSuffix(String logoImageNameSuffix) {
		this.logoImageNameSuffix = logoImageNameSuffix;
	}

	public void setSmallImageNameSuffix(String smallImageNameSuffix) {
		this.smallImageNameSuffix = smallImageNameSuffix;
	}

	public void setMediumImageNameSuffix(String mediumImageNameSuffix) {
		this.mediumImageNameSuffix = mediumImageNameSuffix;
	}

	public void setLargeImageNameSuffix(String largeImageNameSuffix) {
		this.largeImageNameSuffix = largeImageNameSuffix;
	}

	public void setSiteDao(ISiteDao siteDao) {
		this.siteDao = siteDao;
	}

	public void setUserBusiness(IUserBusiness userBusiness) {
		this.userBusiness = userBusiness;
	}

}
